<template>
  <div class="app-container">
    <!-- 筛选查询 -->
    <el-form>
      <!-- 商品id -->
      <el-form-item class="flmr20px">
        <el-input v-model="searchForm.goods_id" style="width:200px" placeholder="请输入商品id" />
      </el-form-item>
      <!-- 商品名称 -->
      <el-form-item class="flmr20px">
        <el-input v-model="searchForm.keyword" style="width:200px" placeholder="请输入商品名称" />
      </el-form-item>
      <!-- 商品分类 -->
      <el-form-item class="flmr20px">
        <el-cascader
          placeholder="请选择商品分类"
          v-model="beforeCate"
          :options="cateOption"
          change-on-select
        />
      </el-form-item>
      <!-- 上架状态 -->
      <el-form-item class="flmr20px">
        <el-select v-model="searchForm.status" placeholder="请选择上架状态">
          <el-option
            v-for="item in statusOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!-- 销量 -->
      <el-form-item class="flmr20px">
        <el-input v-model.number="searchForm.sales_start" style="width:180px" placeholder="请输入真实销量起" onkeyup = "value=value.replace(/[^\d]/g,'')" maxlength="14"/>
        <span>-</span>
        <el-input v-model.number="searchForm.sales_end" style="width:180px" placeholder="请输入真实销量止"  onkeyup = "value=value.replace(/[^\d]/g,'')" maxlength="14"/>
      </el-form-item>
      <!-- 收藏数 -->
      <el-form-item class="flmr20px">
        <el-input v-model.number="searchForm.collectStart" style="width:140px" placeholder="请输入收藏数起" onkeyup = "value=value.replace(/[^\d]/g,'')" maxlength="14"/>
        <span>-</span>
        <el-input v-model.number="searchForm.collectEnd" style="width:140px" placeholder="请输入收藏数止"  onkeyup = "value=value.replace(/[^\d]/g,'')" maxlength="14"/>
      </el-form-item>
      <!-- 购物车 -->
      <el-form-item class="flmr20px">
        <el-input v-model.number="searchForm.cartStart" style="width:140px" placeholder="请输入购物车起" onkeyup = "value=value.replace(/[^\d]/g,'')" maxlength="14"/>
        <span>-</span>
        <el-input v-model.number="searchForm.cartEnd" style="width:140px" placeholder="请输入购物车止"  onkeyup = "value=value.replace(/[^\d]/g,'')" maxlength="14"/>
      </el-form-item>
      <!-- 查询 -->
      <el-form-item class="flmr20px">
        <el-button icon="el-icon-search" type="primary" @click="submitForm()">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="refreshBtn()">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格列表 -->
    <el-table v-loading="goodsForm.loading" :data="goodsList" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="序列" width="">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column align="center" label="商品名称" width="">
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>
      <el-table-column align="header-center" label="创建时间">
        <template slot-scope="scope">{{ scope.row.created_at }}</template>
      </el-table-column>
      <el-table-column align="center" label="一级分类" width="">
        <template slot-scope="scope">{{ scope.row.one_cate }}</template>
      </el-table-column>
      <el-table-column align="center" label="二级分类" width="">
        <template slot-scope="scope">{{ scope.row.two_cate }}</template>
      </el-table-column>
      <el-table-column align="center" label="真实本月销量" width="">
        <template slot-scope="scope">{{ scope.row.month_sales }}</template>
      </el-table-column>
      <el-table-column align="center" label="真实累计销量" width="">
        <template slot-scope="scope">{{ scope.row.total_sales }}</template>
      </el-table-column>
      <el-table-column align="center" label="价格" width="">
        <template slot-scope="scope">{{ scope.row.price }}</template>
      </el-table-column>
      <el-table-column align="center" label="评分" width="">
        <template slot-scope="scope">{{ scope.row.star }}</template>
      </el-table-column>
      <el-table-column align="center" label="收藏数" width="">
        <template slot-scope="scope">{{ scope.row.popularity_count }}</template>
      </el-table-column>
      <el-table-column align="center" label="购物车数" width="">
        <template slot-scope="scope">{{ scope.row.carts }}</template>
      </el-table-column>
      <el-table-column align="center" label="后台销量" width="">
        <template slot-scope="scope">{{ scope.row.sales_num }}</template>
      </el-table-column>
      <el-table-column align="center" label="上架状态" width="">
        <template slot-scope="scope">{{ scope.row.status }}</template>
      </el-table-column>
      <el-table-column align="center" label="首次上架时间" width="">
        <template slot-scope="scope">{{ scope.row.created_at }}</template>
      </el-table-column>
    </el-table>
    <br>
    <el-pagination
      v-show="goodsForm.total>0 && goodsForm.total > goodsForm.limit"
      background
      layout="total, prev, pager, next, sizes"
      :page-sizes="goodsForm.page_size"
      :current-page.sync="goodsForm.page"
      :page-size="goodsForm.limit"
      :total="goodsForm.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

</template>

<script>
import { getGoodsData, getClassifyList } from '@/api/goods'
import Pagination from '@/components/Pagination'

export default {
  name: 'goods',
  components: { Pagination },
  data() {
    return {
    // 用户类型
    cateOption: [],
	  // 上架状态
	  statusOption: [
      {
        value: '0',
        label: '上架'
      }, {
        value: '1',
        label: '下架'
      }
    ],
	  beforeCate: [],
    searchForm: {
      goods_id: '',
      keyword: '',
      cate: '',
      status: '',
      sales_start: '',
      sales_end: '',
      collectStart:'',
      collectEnd:'',
      cartStart:'',
      cartEnd:'',
      loading: false,
      limit: 10,
      page: 1,
      total: 0,
      page_size: [10, 20, 30, 50]
    },
    goodsForm: {
      title: '',
      created_at: '',
      price: '',
      loading: false,
      limit: 10,
      page: 1,
      total: 0,
      page_size: [10, 20, 30, 50]
    },
    dialogVisible: false,
    button_loging: false,
	  goodsList: [],
    flag: 0,
    msgFlag:0
    }
  },
  mounted() {
    this.getGoodsList({ limit: this.goodsForm.limit, page: this.goodsForm.page })
    this.classifyList()
  },
  methods: {
    // 获取商品分类
    classifyList() {
      getClassifyList().then(res => {
        // console.log(res)
        this.cateOption = res.data
      })
    },
    // 获取筛选数据
    submitForm() {
      this.msgFlag = 0
      if(this.searchForm.sales_start>this.searchForm.sales_end||this.searchForm.collectStart>this.searchForm.collectEnd||this.searchForm.cartStart>this.searchForm.cartEnd){
        this.msgFlag = 1
      }
      if(this.msgFlag==0){
        this.searchForm.cate = this.beforeCate[this.beforeCate.length - 1]
        this.goodsForm = this.searchForm
        this.goodsForm.page = 1
        this.screenList(this.goodsForm)
        this.flag = 1
      }else{
        this.alertMsg()
      }
    },
    //弹窗提示
    alertMsg(){
      this.$message({
          message: '起始值不能大于截止值',
          type: 'error'
        });
    },
    // 筛选查询
    screenList(params) {
      this.goodsForm.loading = true
      getGoodsData(params).then(res => {
        this.goodsForm.loading = false
        this.goodsList = res.data.data.message.data
        this.searchForm.total = res.data.data.message.total
        this.searchForm.page = res.data.data.message.current_page
      })
    },
    //重置按钮
    refreshBtn(){
      this.$router.replace({
        path: '/goods',
        query: {
          "": Date.now()
        }
      })
    },
    // 获取商品列表
    getGoodsList(params) {
      this.goodsForm.loading = true
      getGoodsData(params).then(res => {
        this.goodsForm.loading = false
        this.goodsList = res.data.data.message.data
        this.goodsForm.total = res.data.data.message.total
        this.goodsForm.page = res.data.data.message.current_page
      })
    },
    // 更换每页条数
    handleSizeChange(limit) {
      this.goodsForm.limit = limit
      if(this.flag == 0){
        this.getGoodsList({ limit: this.goodsForm.limit, page: this.goodsForm.page })
      }
      if(this.flag == 1){
        this.screenList(this.goodsForm)
      }
    },
    // 切换分页
    handleCurrentChange(page) {
      this.goodsForm.page = page
      if (this.flag == 0) {
        this.getGoodsList({ limit: this.goodsForm.limit, page: this.goodsForm.page })
      }
      if (this.flag == 1) {
        this.screenList(this.goodsForm)
      }
    //   this.goodsForm.page = page
    //   this.getGoodsList({ limit: this.goodsForm.limit, page: this.goodsForm.page })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
.flmr20px{margin-right: 20px;float: left;}
.el-form-item{margin-bottom: 6px!important}
</style>
